<template>
	<view class="box">
		<view class="cu-bar search bg-white">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入你想搜索的" v-model="searchinput"></input>
			</view>
			<view class="action">
				<button class="cu-btn bg-green shadow-blur round" @click="getsearch">搜索</button>
			</view>
		</view>

		<view class="sort">
			<picker :range="array" :value="index" @change="bindPickSort">
				<view class="select">
					<view class="">
						{{array[index]}}
					</view>
					<view class="image">
						<image src="../../static/xiajiantou.png" mode=""></image>
					</view>
				</view>
			</picker>
		</view>
		<view class="list">
			<view class="list_item" v-for="(item,index) in searchlist" :key="index" @click="gotoStore(item)">
				<view class="img">
					<image :src="imgurl + item.image" mode=""></image>
				</view>
				<view class="list_content">
					<text class="shopName">{{item.name}}</text>
					<text class="Grade">{{item.level}}</text>
					<text class="address">{{item.detailAddress}}</text>
				</view>
				<view class="distance">
					{{item.distance}}km
				</view>
			</view>


		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				array: ['距离', '等级', '常用'],
				index: 0,
				InputBottom: 0,
				searchinput: '',
				page: 1, //分页页码
				searchlist: [],
				imgurl: this.$imgurl,
				address: '',
			}
		},
		onLoad() {
			var that = this
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.address);

					var data = {
						longitude: res.longitude,
						latitude: res.latitude,
					}
					that.address = data

				}
			})
		},
		//触底刷新
		bottomingout() {
			if (this.searchlist.length % 8 === 0) {
				this.page++
				this.search()
			} else {
				uni.showToast({
					title: "已经到底了！",
					icon: "none"
				})
				return false
			}
		},
		methods: {
			//搜索
			getsearch() {
				if (this.searchinput.length <= 0) {
					uni.showToast({
						title: "搜索不能为空"
					})
					return false
				}
				this.search()
			},
			// 根据搜索信息请求接口
			search() {
				var that = this
				var data = {
					longitude: that.address.longitude,
					latitude: that.address.latitude,
					order: that.index,
					keywords:that.searchinput

				}
				if (that.index == 2) {
					data.userId = 1
				}
				var http = '/api/user/index/list' + '?limit=' + 8 + '&page=' + that.page
				console.log('请求方式', http)
				that.$http(http, 'POST', data).then((res) => {
					console.log('返回商品列表', res.data.data.list)
					if (res.data.data.list.length <= 0) {
						--that.page
						console.log("已经到底了")
						uni.showToast({
							title: "已经到底了！",
							icon: "none"
						})
						return false
					}
					that.searchlist = that.searchlist.concat(res.data.data.list)
				})
			},
			//门店详情
			gotoStore(item) {
				var data = {
					latitude: this.address.latitude,
					longitude: this.address.longitude,
					storeId:item.id
				}
				uni.navigateTo({
					url: "../stroeDetails/stroeDetails?data=" + JSON.stringify(data)
				})
			},
			bindPickSort: function(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.page = 1
				this.searchlist = []
				this.search()
			},
		}
	}
</script>

<style lang="scss">
	.box {
		width: 100%;
		height: 100%;

		.sort {
			font-size: 27rpx;
			padding: 20rpx;
			border: 1rpx solid #DCDFE6;

			.select {
				display: flex;
				align-items: center;

				.image {
					width: 30rpx;
					height: 30rpx;
					margin-left: 10rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.list {
			width: 750rpx;
			margin-top: 20rpx;

			.list_item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 10rpx;
				padding: 20rpx 40rpx;
				font-size: 25rpx;
				border-bottom: 1rpx solid #ECECEC;

				.list_content {
					flex: 0 0 50%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					line-height: 50rpx;

					.shopName {
						font-size: 32rpx;
						color: rgba(0, 0, 0, 0.76);
						font-weight: 550;
					}

					.Grade {
						width: 104rpx;
						height: 40rpx;
						background-color: #f49c1c;
						color: #FFFFFF;
						border-radius: 50rpx;
						font-size: 20rpx;
						display: flex;
						align-items: center;
						justify-content: center;
					}

					.address {
						width: 400rpx;
						color: #7f7f7f;
						font-size: 26rpx;
						overflow: hidden;
						/*超出部分隐藏*/
						white-space: nowrap;
						/*不换行*/
						text-overflow: ellipsis;
						/*超出部分文字以...显示*/
					}
				}

				.distance {
					flex: 0 0 20%;
					text-align: center;
					color: #a3a3a3;
					font-size: 24rpx;
				}

				.img {
					width: 130rpx;
					height: 130rpx;
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
</style>
